//
//  APIClient.m
//  YDdDemo
//
//  Created by 瞿伦平 on 2017/10/20.
//  Copyright © 2017年 瞿伦平. All rights reserved.
//

#import "APIClient.h"

@implementation APIClient
+ (instancetype)sharedClient {
    static APIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://ydd.xtangguo.com"]];
        //        _sharedClient.requestSerializer.HTTPShouldHandleCookies = YES;
        _sharedClient.responseSerializer = [AFHTTPResponseSerializer serializer];
        _sharedClient.requestSerializer = [AFHTTPRequestSerializer serializer];
    });
    return _sharedClient;
}


/**
 *  获取评论
 *
 *  @param iD      店铺id
 *  @param callback 返回列表
 */
-(void)comentListWithShopId:(NSString *)iD call:(TableBlock)callback
{
    
//    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcomment/cleaning/merchantid/%@", urlHead, iD];
    
    NSString *url;
    NSString *state = [user objectForKey:@"gxsg"];
    if ([state isEqualToString:@"gx"]) {//干洗评论
        url = [NSString stringWithFormat:@"%@/App/Merchantcomment/cleaning/merchantid/%@", urlHead, iD];
    }else{//蔬果评论接口
        url = [NSString stringWithFormat:@"%@/App/Merchantcomment/vegetables/merchantid/%@",urlHead,iD];
    }
    
    [self POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        APIObject *info = [APIObject mj_objectWithKeyValues:responseObject];
        NSArray *aaArr = (NSArray *)info.data;
        if (aaArr.count > 0) {
            NSMutableArray *arr = [OrderCommentObject mj_objectArrayWithKeyValuesArray:aaArr];
            callback(arr, info);
        } else
            callback(nil, info);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        callback(nil, [APIObject infoWithError:error]);
    }];
}


/**
 *  获取分类商品
 *
 *  @param iD      店铺id
 *  @param callback 返回列表
 */
-(void)goodsListWithShopId:(NSString *)iD call:(TableBlock)callback
{
    //
//    NSString *url = [NSString stringWithFormat:@"%@/App/Cleaning/details/merchantid/%@", urlHead, iD];
    
    NSString *url;
    NSString *state = [user objectForKey:@"gxsg"];
    if ([state isEqualToString:@"gx"]) {
        url = [NSString stringWithFormat:@"%@/App/Cleaning/details/merchantid/%@", urlHead, iD];
    }else{//蔬果接口
        url = [NSString stringWithFormat:@"%@/App/Vegetables/details/merchantid/%@",urlHead,iD];
    }
    
    [self POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        APIObject *info = [APIObject mj_objectWithKeyValues:responseObject];
        NSArray *aaArr = (NSArray *)info.data;
        NSLog(@"arr:%@",aaArr);
        if (aaArr.count > 0) {
            NSMutableArray *arr = [FoodCate mj_objectArrayWithKeyValuesArray:aaArr];
            callback(arr, info);
        } else
            callback(nil, info);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        callback(nil, [APIObject infoWithError:error]);
    }];
}



/**
 *  获取配送费
 *
 *  @param callback 返回信息--{"id":"1","cost":"6.00","km":"1","mixkm":"6","minxcost":"2.00","startkm":"2"}
 */
-(void)getShippingfeeCall:(void (^)(APIObject* info))callback
{
    NSString *url = [NSString stringWithFormat:@"%@/App/Blitzcrank/shippingfee",urlHead];
    [self POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        APIObject *info = [APIObject mj_objectWithKeyValues:responseObject];
        if (info.data != nil) {
            Shippingfee *item = [Shippingfee mj_objectWithKeyValues:info.data];
            if (item != nil) {
                //保存数据库
                [LKDBHelper clearTableData:[Shippingfee class]];
                LKDBHelper* globalHelper = [Shippingfee getUsingLKDBHelper];
                [globalHelper insertToDB:item];
            }
        }
        callback(info);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        callback([APIObject infoWithError:error]);
    }];
}


/**
 *  获取距离
 *
 *  @param callback 返回信息--{"id":"1","cost":"6.00","km":"1","mixkm":"6","minxcost":"2.00","startkm":"2"}
 */
-(void)getKmWithStart:(CLLocationCoordinate2D)start end:(CLLocationCoordinate2D)end call:(void (^)(float km, APIObject* info))callback
{
    NSString *url = [NSString stringWithFormat:@"%@/App/Banner/km",urlHead];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:[NSString stringWithFormat:@"%f", start.longitude] forKey:@"start_longitude"];
    [parmas setObject:[NSString stringWithFormat:@"%f", start.latitude] forKey:@"start_latitude"];
    [parmas setObject:[NSString stringWithFormat:@"%f", end.longitude] forKey:@"end_longitude"];
    [parmas setObject:[NSString stringWithFormat:@"%f", end.latitude] forKey:@"end_latitude"];
    [self POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        APIObject *info = [APIObject mj_objectWithKeyValues:responseObject];
        if (info.data != nil) {
            float aa = [[info.data objectWithKey:@"km"] floatValue];
            callback(aa, info);
        }
        callback(0, info);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        callback(0, [APIObject infoWithError:error]);
    }];
}




@end
